package com.example.sqliteviewer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class TableAdapter extends RecyclerView.Adapter<TableAdapter.TableViewHolder> {

    private final Context context;
    private final List<String> tableNames;
    private final OnTableClickListener listener;

    public interface OnTableClickListener {
        void onTableClick(String tableName);
    }

    public TableAdapter(Context context, List<String> tableNames, OnTableClickListener listener) {
        this.context = context;
        this.tableNames = tableNames;
        this.listener = listener;
    }

    @NonNull
    @Override
    public TableViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_table, parent, false);
        return new TableViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TableViewHolder holder, int position) {
        String tableName = tableNames.get(position);
        holder.tvTableName.setText(tableName);
        
        holder.cardView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onTableClick(tableName);
            }
        });
    }

    @Override
    public int getItemCount() {
        return tableNames.size();
    }

    static class TableViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        TextView tvTableName;

        public TableViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.cardView);
            tvTableName = itemView.findViewById(R.id.tvTableName);
        }
    }
}